﻿///////////////////////////////////////////////////////////////////////////
//内容评论扩展插件
///////////////////////////////////////////////////////////////////////////
var ajaxPluginCommentPageSize = 5;//每页显示的评论数
function ajaxPluginComment(ccid,id)
{
	if(PluginComment==null)
	{
		$("#ajaxPluginComment").hide();
		return;
	}
	$.ajax({
		type:		"get",
		dataType:	"json",
		data:		"oper=ajaxPluginComment&id="+id+"&ccid="+ccid+"&time="+(new Date().getTime()),
		url:		site.Dir + "extends/comment/ajax.aspx",
		error:		function(XmlHttpRequest,textStatus, errorThrown){if(XmlHttpRequest.responseText!=""){alert(XmlHttpRequest.responseText);}},
		success:	function(d){
			$("#ajaxPluginComment").setTemplateURL(site.Dir + "extends/comment/templates/_comment.htm?time="+(new Date().getTime()), null, {filter_data: true});
			$("#ajaxPluginComment").processTemplate(d);
		}
	});
}
function ajaxPluginCommentCount(ccid,id)
{
	$.ajax({
		type:		"get",
		dataType:	"json",
		data:		"oper=ajaxPluginCommentCount&id="+id+"&ccid="+ccid+"&time="+(new Date().getTime()),
		url:		site.Dir + "extends/comment/ajax.aspx",
		error:		function(XmlHttpRequest,textStatus, errorThrown){if(XmlHttpRequest.responseText!=""){alert(XmlHttpRequest.responseText);}},
		success:	function(d){
			$("#ajaxPluginCommentCount_"+id).text(d.count);
		}
	});
}
function ajaxPluginCommentAdd(ccid,id)
{
	var uName=$("#commentName").val();
	var uCode=$("#commentCode").val();
	var parentid=$("#commentParentId").val();
	var content=$("#commentContent").val();
	if(!uName) {
		alert("用户名不能为空!");
		return;
	}
	if(uName.length>16){
		alert("用户名太长!");
		return;
	}
	if(uCode != ReadCookie("ValidateCode")){
		alert('验证码错误');
		return;
	}
	if(!content || content.length<5) {
		alert("评论字符太少!");
		return;
	}
	if(content.length>200){
		alert("评论字符太多!");
		return;
	}
	$("#btnCommentAdd").attr("disabled","disabled");
	
	$.ajax({
		type:		"post",
		dataType:	"html",
		data:		"ccid="+ccid+"&id="+id+"&parentid="+parentid+"&name="+escape(uName)+"&code="+escape(uCode)+"&content="+escape(content),
		url:		site.Dir + "extends/comment/ajax.aspx?oper=ajaxPluginCommentAdd&time="+(new Date().getTime()),
		error:		function(XmlHttpRequest,textStatus, errorThrown){if(XmlHttpRequest.responseText!=""){alert(XmlHttpRequest.responseText);}},
		success:	function(d){
			if(d=="ok")
			{
				$("#commentContent").val("");
				$("#commentCode").val("");
				if(parentid=="0")
					alert("成功发表评论");
				else
					alert("成功回复评论");
				ajaxPluginCommentList(ccid,id,1);
			}
			else
			{
				alert(d);
				//$("#ajaxPluginComment").hide();
				//$("#btnCommentAdd").attr("disabled","");
			}
		}
	});
}
function ajaxPluginCommentList(ccid,id,page,pagesize)
{
	if(!pagesize) pagesize = ajaxPluginCommentPageSize;
	$.ajax({
		type:		"get",
		dataType:	"html",
		data:		"oper=ajaxPluginCommentList&ccid="+ccid+"&id="+id+"&page="+page+"&pagesize="+pagesize+"&time="+(new Date().getTime()),
		url:		site.Dir + "extends/comment/ajax.aspx",
		error:		function(XmlHttpRequest,textStatus, errorThrown){if(XmlHttpRequest.responseText!=""){alert(XmlHttpRequest.responseText);}},
		success:	function(d){
			$("#ajaxPluginCommentList").html(d);
		}
	});
}
function ajaxPluginCommentTopList(ccid,id,pagesize,elementid)
{
	$.ajax({
		type:		"get",
		dataType:	"html",
		data:		"oper=ajaxPluginCommentTopList&ccid="+ccid+"&id="+id+"&page=1&pagesize="+pagesize+"&time="+(new Date().getTime()),
		url:		site.Dir + "extends/comment/ajax.aspx",
		error:		function(XmlHttpRequest,textStatus, errorThrown){if(XmlHttpRequest.responseText!=""){alert(XmlHttpRequest.responseText);}},
		success:	function(d){
			$('#'+elementid).html(d);
		}
	});
}
function ajaxPluginCommentReply2(parentid)
{
	$("#commentParentId").val(parentid);
	$("#commentContent").val("");
	$("#commentContent")[0].focus();
}
function ajaxPluginCommentUserInfo()
{
	$.ajax({
		type:		"get",
		dataType:	"json",
		data:		"oper=ajaxPluginCommentUserInfo&time="+(new Date().getTime()),
		url:		site.Dir + "extends/comment/ajax.aspx",
		error:		function(XmlHttpRequest,textStatus, errorThrown){if(XmlHttpRequest.responseText!=""){alert(XmlHttpRequest.responseText);}},
		success:	function(data){
			if(data.username=="")
			{
				$("#commentName").val("匿名游客");
				$(".commentName").html("匿名游客");
				$("#guest_btn").show();
			}
			else{
				$("#commentName").val(data.username);
				$(".commentName").html(data.username);
			}
		}
	});
}